﻿using HandyControl.Controls;
using MT.ImageTool.GaoDeInfrared;
using System.IO;
using System.Windows.Media.Imaging;
using MessageBox = HandyControl.Controls.MessageBox;

namespace MT.ImageTool
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private GaoDeImageJPGModel? CurGaoDeImageModel;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Btn_LoadImage_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "images (*.jpg)|*.jpg";
            openFileDialog.RestoreDirectory = true;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string fileName = openFileDialog.FileName;
                image.Source = new BitmapImage(new Uri(fileName, UriKind.RelativeOrAbsolute));

                CurGaoDeImageModel = GaoDeInfraredImageHelper.GetGaoDeImageJPGModel(fileName);
                if (CurGaoDeImageModel == null) 
                {
                    MessageBox.Show("红外图像解析失败！");
                    return;
                }

                image_name.Text = Path.GetFileName(fileName);
                image_max_temper.Text = CurGaoDeImageModel.MaxTemper.ToString("F1");
                image_min_temper.Text = CurGaoDeImageModel.MinTemper.ToString("F1");
                image_avg_temper.Text = CurGaoDeImageModel.AvgTemper.ToString("F1");
            }
        }

        private void image_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (CurGaoDeImageModel != null)
            {
                System.Windows.Point mousePos = e.GetPosition(image);

                int wIndex = (int)(mousePos.X / image.ActualWidth * (double)CurGaoDeImageModel.ImageWidth);
                int hIndex = (int)(mousePos.Y / image.ActualHeight * (double)CurGaoDeImageModel.ImageHeight);
                int tempIndex = hIndex * CurGaoDeImageModel.ImageWidth + wIndex;

                float tempervture = 0;
                if (CurGaoDeImageModel.TemperFloat.Count > tempIndex)
                {
                    tempervture = CurGaoDeImageModel.TemperFloat[tempIndex];
                }
                //image_Mouse_temper.Text = $"{wIndex}, {hIndex} {mousePos.X}, {mousePos.Y} {tempervture:F2}";
                image_Mouse_temper.Text = $"{tempervture:F1}";
            }
        }
    }
}